<?php
$all=[
    0 => "account_settings",
    1 => "addons",
    2 => "addresses",
    3 => "billing_details",
    4 => "blog_categories",
    5 => "blog_category_link",
    6 => "blog_tag_links",
    7 => "blog_tags",
    8 => "blogs",
    9 => "booking_logs",
    10 => "cache",
    11 => "cache_locks",
    12 => "cart_items",
    13 => "categories",
    14 => "countries",
    15 => "country_states",
    16 => "dispute_conversations",
    17 => "disputes",
    18 => "email_templates",
    19 => "failed_jobs",
    20 => "favourite_users",
    21 => "job_batches",
    22 => "jobs",
    23 => "languages",
    24 => "lg__attachments",
    25 => "lg__chat_actions",
    26 => "lg__friends",
    27 => "lg__gp_users",
    28 => "lg__guest_accounts",
    29 => "lg__messages",
    30 => "lg__notifications",
    31 => "lg__seen_messages",
    32 => "lg__thread_details",
    33 => "lg__thread_participants",
    34 => "lg__threads",
    35 => "ltu_languages",
    36 => "ltu_phrases",
    37 => "ltu_translation_files",
    38 => "ltu_translations",
    39 => "menu",
    40 => "menu_items",
    41 => "migrations",
    42 => "model_has_permissions",
    43 => "model_has_roles",
    44 => "optionbuilder__settings",
    45 => "order_items",
    46 => "orders",
    47 => "pagebuilder__pages",
    48 => "password_reset_tokens",
    49 => "password_resets",
    50 => "permissions",
    51 => "personal_access_tokens",
    52 => "profiles",
    53 => "ratings",
    54 => "role_has_permissions",
    55 => "roles",
    56 => "sessions",
    57 => "slot_bookings",
    58 => "social_profiles",
    59 => "subject_groups",
    60 => "subjects",
    61 => "telescope_entries",
    62 => "telescope_entries_tags",
    63 => "telescope_monitoring",
    64 => "user_certificate",
    65 => "user_contacts",
    66 => "user_education",
    67 => "user_experience",
    68 => "user_identity_verifications",
    69 => "user_languages",
    70 => "user_payout_methods",
    71 => "user_subject_group_subjects",
    72 => "user_subject_groups",
    73 => "user_subject_slots",
    74 => "user_wallet_details",
    75 => "user_wallets",
    76 => "user_withdrawals",
    77 => "users",
  ] ;
$sontofather= [
    "account_settings" => "users",
    "addresses" => "countries",
    "billing_details" => "users",
    "blogs" => "users",
    "booking_logs" => "slot_bookings",
    "country_states" => "countries",
    "dispute_conversations" => "disputes",
    "disputes" => "users",
    "favourite_users" => "users",
    "ltu_phrases" => "ltu_phrases",
    "model_has_permissions" => "permissions",
    "model_has_roles" => "roles",
    "order_items" => "orders",
    "profiles" => "users",
    "ratings" => "users",
    "role_has_permissions" => "permissions",
    "slot_bookings" => "users",
    "social_profiles" => "users",
    "telescope_entries_tags" => "telescope_entries",
    "user_certificate" => "users",
    "user_contacts" => "users",
    "user_education" => "countries",
    "user_experience" => "countries",
    "user_identity_verifications" => "users",
    "user_languages" => "languages",
    "user_payout_methods" => "users",
    "user_subject_group_subjects" => "subjects",
    "user_subject_groups" => "subject_groups",
    "user_subject_slots" => "user_subject_group_subjects",
    "user_wallet_details" => "user_wallets",
    "user_wallets" => "users",
    "user_withdrawals" => "users",
  ] ;

  function topologicalSort($graph) {
    $inDegree = [];
    // 计算每个顶点的入度
    foreach ($graph as $node => $neighbors) {
        if (!isset($inDegree[$node])) {
            $inDegree[$node] = 0;
        }
        foreach ($neighbors as $neighbor) {
            if (!isset($inDegree[$neighbor])) {
                $inDegree[$neighbor] = 0;
            }
            $inDegree[$neighbor]++;
        }
    }

    // 初始化队列，将入度为 0 的顶点加入队列
    $queue = [];
    foreach ($inDegree as $node => $degree) {
        if ($degree === 0) {
            $queue[] = $node;
        }
    }

    $result = [];
    // 进行拓扑排序
    while (!empty($queue)) {
        $node = array_shift($queue);
        $result[] = $node;
        // 减少相邻顶点的入度
        if (isset($graph[$node])) {
            foreach ($graph[$node] as $neighbor) {
                $inDegree[$neighbor]--;
                if ($inDegree[$neighbor] === 0) {
                    $queue[] = $neighbor;
                }
            }
        }
    }

    // 检查是否存在环
    if (count($result) === count($inDegree)) {
        return $result;
    } else {
        return null; // 图中存在环，无法进行拓扑排序
    }
}

$one=array_diff($all,array_keys($sontofather));
$two=topologicalSort($sontofather);
echo "没有依赖表". count($one) ."\n";
print_r($one);
echo "拓扑排序". count($two) ."\n";
print_r($two);
$end=array_merge($one,$two);
echo "最终". count($end) ."\n";
print_r($end);